package com.atguigu.eduservice.controller;

import com.atguigu.common.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQueryVo;
import com.atguigu.eduservice.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Panghl
 * @since 2020-12-25
 */
@Api(tags = {"讲师管理"})
@RestController
@RequestMapping("/eduservice/teacher")
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "查询所有")
    @GetMapping("findAll")
    public R findAll() {
        return R.ok().data("teacherList",teacherService.list(null));
    }


    @ApiOperation(value = "根据id获取讲师信息")
    @GetMapping("/get/{id}")
    public R get(@PathVariable("id") String id) {
        return R.ok().data("teacher", teacherService.get(id));
    }

    @ApiOperation(value = "带条件的分页查询")
    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable("current") long current,
                                  @PathVariable("limit") long limit,
                                  @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
        return R.ok().data(teacherService.page(current,limit,teacherQueryVo));
    }

    @ApiOperation(value = "添加讲师")
    @PostMapping("/add")
    public R add(@RequestBody EduTeacher eduTeacher) {
        if (teacherService.save(eduTeacher)) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "修改讲师")
    @PostMapping("/edit")
    public R edit(@RequestBody EduTeacher eduTeacher) {
        if (teacherService.updateById(eduTeacher)) {
            return R.ok();
        }
        return R.error();
    }

    @ApiOperation(value = "删除讲师")
    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable String id) {
      if (teacherService.removeById(id)) {
        return R.ok();
      }
      return R.error();
    }

}
